import {
    Inject, Controller, Post, Get,
    Body, Query,
    Param
} from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { RoleService } from '../service/role.service';
import { Role } from '../entity/role.entity';

@Controller('/role')
export class RoleController {
    @Inject()
    ctx: Context;

    @Inject()
    roleService: RoleService;

    @Post('/create')
    async create(@Body() role: Partial<Role>) {
        try {
            const result = await this.roleService.create(role);
            return {
                code: 200,
                message: '创建成功',
                data: result
            };
        } catch (error) {
            return {
                code: 500,
                message: error.message || '创建失败'
            };
        }
    }

    @Post('/update')
    async update(@Body() role: Partial<Role>) {
        try {
            const success = await this.roleService.update(role.id, role);
            return {
                code: 200,
                message: success ? '更新成功' : '更新失败'
            };
        } catch (error) {
            return {
                code: 500,
                message: error.message || '更新失败'
            };
        }
    }

    @Get('/del')
    async delete(@Query('id') id: number,) {
        try {
            const success = await this.roleService.delete(id);
            return {
                code: 200,
                message: success ? '删除成功' : '删除失败'
            };
        } catch (error) {
            return {
                code: 500,
                message: error.message || '删除失败'
            };
        }
    }

    @Get('/detail')
    async getInfo(@Query('id') id: number) {
        try {
            const role = await this.roleService.getInfo(id);
            return {
                code: 200,
                message: '获取成功',
                data: role
            };
        } catch (error) {
            return {
                code: 500,
                message: error.message || '获取失败'
            };
        }
    }

    @Get('/list')
    async getList(
        @Query('page') page: number,
        @Query('pageSize') pageSize: number,
        @Query('roleName') roleName?: string,
        @Query('roleKey') roleKey?: string,
        @Query('status') status?: number
    ) {
        try {
            const [list, total] = await this.roleService.getList({
                page,
                pageSize,
                roleName,
                roleKey,
                status
            });

            return {
                code: 200,
                message: '获取成功',
                data: list,
                total
            };
        } catch (error) {
            return {
                code: 500,
                message: error.message || '获取失败'
            };
        }
    }

    /**
     * 为角色分配权限
     */
    @Post('/assignPermissions')
    async assignPermissions(
        @Body('roleId') roleId: number,
        @Body('permIds') permIds: number[]
    ) {
        try {
            const success = await this.roleService.assignPermissions(roleId, permIds);
            return {
                code: 200,
                message: success ? '分配权限成功' : '分配权限失败'
            };
        } catch (error) {
            return {
                code: 500,
                message: error.message || '分配权限失败'
            };
        }
    }

    /**
     * 获取角色已分配的权限
     */
    @Get('/permissions/:roleId')
    async getRolePermissions(@Param('roleId') roleId: number) {
        try {
            const permissions = await this.roleService.getRolePermissions(roleId);
            return {
                code: 200,
                message: '获取成功',
                data: permissions
            };
        } catch (error) {
            return {
                code: 500,
                message: error.message || '获取失败'
            };
        }
    }

    /**
     * 移除角色的权限
     */
    @Post('/removePermissions')
    async removePermissions(
        @Body('roleId') roleId: number,
        @Body('permIds') permIds: number[]
    ) {
        try {
            const success = await this.roleService.removePermissions(roleId, permIds);
            return {
                code: 200,
                message: success ? '移除权限成功' : '移除权限失败'
            };
        } catch (error) {
            return {
                code: 500,
                message: error.message || '移除权限失败'
            };
        }
    }
}